class Solution {
    public boolean isMatch(String s, String p) {
        int n = s.length() ;
        int m = p.length() ;
        boolean[][] dp = new boolean[n+1][m+1]; // [0,j]的字符能否匹配s[0,i]的字符]
        s = " "+s ;
        p = " "+p ;
        char[] sArr = s.toCharArray() ;
        char[] pArr = p.toCharArray() ;
        dp[0][0] =true ;                // 刚开头的第一个空字符就是true
        for(int j=1; j<=m;j++){        // 对开头的连续*,进行处理
            if(pArr[j] == '*' && dp[0][j-1] ==true){
                dp[0][j] = true ;
            }
        }
        for(int i=1 ; i <= n ; i ++){
            for(int j=1 ; j <=m ; j ++){
                if(pArr[j] !='*' && pArr[j]!='?' ){
                    dp[i][j] = sArr[i]==pArr[j] && dp[i-1][j-1] ;
                }else{
                    if(pArr[j] == '?'){
                        dp[i][j] = dp[i-1][j-1] ;
                    }else{
                        dp[i][j] = dp[i][j-1] ;
                        for(int y=1 ; y<=i && dp[i][j]!=true ; y ++){
                            dp[i][j] = dp[i][j] || dp[i-y][j-1];
                        }
                    }
                }
            }
        }
        return dp[n][m] ;
    }
}